(function(){
var replacement = '\u25CF';
function PseudoPasswordOfMobileManer(input, option){
	var defaultOption = {
		'delay':  1000
	}
	if( !option ){
		option = {}
	}
	for( i in defaultOption ){
		if( i in option ){
			defaultOption[i] = option[i];
		}
	}
	this.option = defaultOption;
	this.input = input;
	this.text = input.value || '';
	this.time = 0;
	this.interval = 50;
	this.t = null;
	this.showLastChar = true;
}
ppmm = PseudoPasswordOfMobileManer;
ppmm.prototype = {
	getText: function(){
		return this.text;
	},
	translate: function(){
		addEventListener(this.input, 'focus', bind(this.startCheck, this));
		addEventListener(this.input, 'blur', bind(this.endCheck, this));
	//	addEventListener(this.input, 'click', bind(this.removeInputMode, this));
	},
	_translate: function(){
		var i = this.input;
		this.time += this.interval;
		var keepLast = this.time < this.option.delay;
		if( this.time >= this.option.delay ){
			this.time = 0;
		}
		var asterisk = this.asterisk(i.value, keepLast);
		if( this.input.selectionStart ){
			var s = i.selectionStart,
				e = i.selectionEnd;
			if( i.value != asterisk ){
				i.value = asterisk;
				i.selectionStart = s < e ? s : e;
				i.selectionEnd = e >= s ? e : s;
			}
		}else{
			if( i.value != asterisk ){
				var k = 0, v = i.value, len = v.length;
				while( k < len && v.charAt(k) === replacement ) k++;
				while( k < len && v.charAt(k) !== replacement ) k++;
				var len = i.value.length - k;
				//console.log(i.value, asterisk, k, len)
				i.value = asterisk;
				var sel = document.selection.createRange();
				sel.moveStart('character', -len);
				sel.moveEnd('character', -len);
				sel.select();
			}
		}
	},
	asterisk: function(str, keepLast){
		var as = [], rs = [], t = this.text, k = this.text.length - 1;
		for( var i = str.length - 1; i >= 0; i-- ){
			if( str.charAt(i) != replacement && keepLast){
				as.unshift( str.charAt(i) );
				keepLast = false;
			}else{
				as.unshift(replacement)
			}
		}
		str = as.join('')
		if( str.length != this.text.length ){
			var start = 0, end = 0;
			while( start < str.length && str.charAt(start) == replacement ) start++;
			end = start;
			while( end < str.length && str.charAt(end) != replacement ) end++;
			this.text = this.text.substring(0, start) +
				str.substring(start, end) +
				this.text.substr(start, str.length - end);
		}
		//console.log('a', this.text, str, start, end)
		return as.join('');
	},
	startCheck: function(){
		if(!this.t)
			this.t = setInterval( bind(this._translate, this), this.interval );
	},
	endCheck: function(){
		//this.t && clearInterval(this.t);
		//this.t = null;
	},
	dummy: null
}

function bind(handler, obj){
	return function(){
		handler.apply(obj, arguments);
	}
}

function addEventListener(obj, ev, handler){
	if( obj.addEventListener ){
		obj.addEventListener( ev,  handler, false );
	}else if( obj.attachEvent ){
		obj.attachEvent( 'on' + ev, function(){ var e = window.event; handler.call(this, e); } );
	}
}
window.ppmm = ppmm;
})();
